package com.boat.starter.ruoyi.client;

import java.util.Set;

import com.boat.starter.ruoyi.client.response.RmsResponse;
import com.boat.starter.ruoyi.client.response.SyncConfigResp;
import com.boat.starter.ruoyi.config.props.AuthProperties;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * rms客户端
 * 
 * @author 周江
 *
 */
@Slf4j
public class RuoyiClient {
	private static final String SYNCCONFIG_URI = "/starter/syncConfig";
	private static final String REFRESHTOKENS_URI = "/starter/refreshTokens";

	private AuthProperties authProperties;

	public RuoyiClient(AuthProperties authProperties) {
		this.authProperties = authProperties;
	}

	@SuppressWarnings("unchecked")
	public RmsResponse<SyncConfigResp> syncConfig() {
		String url = authProperties.getRuoyi().getServerAddr() + SYNCCONFIG_URI;
		log.info("同步配置。url={}", url);
		String result = HttpUtil.get(url);
		log.info("同步配置结果：{}", result);
		return JSONUtil.parse(result).toBean(new RmsResponse<SyncConfigResp>() {
		}.getClass());
	}

	@SuppressWarnings("unchecked")
	public RmsResponse<Void> refreshTokens(final Set<String> tokens) {
		if (tokens == null && tokens.isEmpty()) {
			log.info("tokens数据为空，直接返回");
			return new RmsResponse<Void>();
		}

		String url = authProperties.getRuoyi().getServerAddr() + REFRESHTOKENS_URI;
		log.info("tokens续约请求。url={}, tokens.size={}", url, tokens.size());
		String result = HttpUtil.post(url, JSONUtil.toJsonStr(tokens));
		log.info("tokens续约结果：{}", result);
		return JSONUtil.parse(result).toBean(new RmsResponse<Void>() {
		}.getClass());
	}

}
